package org.cwnuit.train.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.cwnuit.train.domain.Learns;
import org.cwnuit.train.domain.Rules;
import org.cwnuit.train.utils.ConnDB;
import org.cwnuit.train.utils.EntityUtil;
import org.cwnuit.train.utils.PageModel;
@SuppressWarnings("all")
public class LearnDAO {
	
	private static Logger logger = Logger.getLogger(LearnDAO.class.getName());   

	// 添加學習情況信息
	public boolean addLearn(String SQL) {
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		boolean flag = false;
		try {
			stmt.executeUpdate(SQL);
			flag = true;
			logger.debug(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnDB.closeStatement(stmt);
			ConnDB.closeConnection(conn);
		}
		return flag;
	}

	// 删除學習情況信息
	public boolean deleteLearn(String SQL) {
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		boolean flag = false;
		try {
			stmt.execute(SQL);
			flag = true;
			logger.debug(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnDB.closeStatement(stmt);
			ConnDB.closeConnection(conn);
		}
		return flag;
	}

	// 修改學習情況信息
	public boolean updateLearn(String SQL) {
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		boolean flag = false;
		try {
			stmt.executeUpdate(SQL);
			flag = true;
			logger.debug(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnDB.closeStatement(stmt);
			ConnDB.closeConnection(conn);
		}
		return flag;
	}
	
	// 查找Learn的方法
	public Learns findLearn(String SQL) {

		Learns learn = null;
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		ResultSet rs = ConnDB.getResultSet(stmt, SQL);

		try {
			while (rs.next()) {
				learn = EntityUtil.RsToLearn(rs);
			}
			logger.debug(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnDB.closeStatement(stmt);
			ConnDB.closeResultSet(rs);
			ConnDB.closeConnection(conn);
		}
		return learn;
	}
	// 查找Learn的方法
	public List<Learns> findLearns(String SQL) {
		List<Learns> learnslist = new ArrayList();
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		ResultSet rs = ConnDB.getResultSet(stmt, SQL);

		try {
			while (rs.next()) {
				learnslist.add(EntityUtil.RsToLearn(rs));
			}
			logger.debug(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnDB.closeStatement(stmt);
			ConnDB.closeResultSet(rs);
			ConnDB.closeConnection(conn);
		}
		return learnslist;
	}
}
